package com.jht.bletool2java.characteristic.userdata;

import android.bluetooth.BluetoothGattCharacteristic;
import android.util.Log;

import androidx.annotation.Keep;


import com.jht.bletool2java.characteristic.TranslateData;
import com.jht.bletool2java.util.ByteUtil;

import top.codestudy.annotation_uuid.MyUUID;

@Keep
@MyUUID(uuid = "00002a8c-0000-1000-8000-00805f9b34fb")
public class Gender implements TranslateData {
    private static final String TAG = "Gender";
    private String gender = "";
    private byte[] value;
    private boolean isInvalidData = false;
    public Gender() {
    }

    public Gender(BluetoothGattCharacteristic characteristic) {
        if (characteristic == null){
            Log.w(TAG, "Gender: characteristic is null!");
            isInvalidData = true;
            return;
        }
        value = characteristic.getValue();
        isInvalidData = (value == null || value.length < 1);
        if (isInvalidData){
            gender = "no data!";
            Log.w(TAG, "Gender: value == null || value.length < 1");
        }else {
            int i = ByteUtil.byte1ToInt(value[0]);
            switch (i){
                case 0:
                    gender = "Male";
                    break;
                case 1:
                    gender = "Female";
                    break;
                case 2:
                    gender = "Unspecified";
                    break;
                default:
                    gender = "don't have this field";
                    break;
            }
        }



    }

    public Gender(byte[] value) {
        this.value = value;
        int i = ByteUtil.byte1ToInt(value[0]);
        switch (i){
            case 0:
                gender += "Male";
                break;
            case 1:
                gender += "Female";
                break;
            case 2:
                gender += "Unspecified";
                break;
            default:
                gender += "don't have this field";
                break;
        }

    }
    @Override
    public String convert2String() {
        return "Gender: " + gender+"\n";
    }

    @Override
    public String merge(TranslateData translateData) {
        return null;
    }

    @Override
    public boolean hasMoreData() {
        return false;
    }

    @Override
    public byte[] getData() {
        return value;
    }

    @Override
    public boolean isInvalidData() {
        return isInvalidData;
    }

    @Override
    public void parseData(BluetoothGattCharacteristic characteristic, byte[] value) {
        isInvalidData = (value == null || value.length < 1);
        if (isInvalidData) {
            //数据非法
            this.value = value;
            gender = "";
            Log.w(TAG, "parseData: value == null || value.length < 1");
        }else {
            this.value = value;
            int i = ByteUtil.byte1ToInt(value[0]);
            switch (i){
                case 0:
                    gender  = "Male";
                    break;
                case 1:
                    gender  = "Female";
                    break;
                case 2:
                    gender  = "Unspecified";
                    break;
                default:
                    gender  = "don't have this field";
                    break;
            }
        }

    }
}
